package com.siao.core
{
	import com.siao.data.HashMap;
	import com.siao.errors.SingletonError;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.media.Sound;
	import flash.system.ApplicationDomain;
	import flash.text.Font;
	
	/**
	 * 资源管理器
	 * @author SiaoLeon
	 */
	public class AssetsManager
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		private static var _instance:AssetsManager;
		
		/**
		 * 获取单例对象
		 */
		public static function get instance():AssetsManager
		{
			if (_instance == null)
			{
				_instance = new AssetsManager();
			}
			return _instance;
		}
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var assets:HashMap;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数，单例模式
		 */
		public function AssetsManager()
		{
			if (_instance != null)
			{
				throw new SingletonError(this);
			}
			_instance = this;
			
			this.init();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 保存资源
		 * @param name 资源名称
		 * @param asset 资源对象
		 */
		public function saveAsset(name:String, asset:*):void
		{
			this.assets.put(name, asset);
		}
		
		/**
		 * 获取资源
		 * @param name 资源名称
		 * @return
		 *
		 */
		public function getAsset(name:String):*
		{
			return this.assets.getValue(name);
		}
		
		/**
		 * 删除资源
		 * @param name 资源名称
		 */
		public function removeAsset(name:String):void
		{
			this.assets.remove(name);
		}
		
		/**
		 * 清除所有资源
		 */
		public function clearAsset():void
		{
			this.assets.clear();
		}
		
		/**
		 * 获取显示对象实例
		 * @param name 实例链接名
		 * @return
		 */
		public function getDefinitionDisplayObject(name:String):DisplayObject
		{
			if (!ApplicationDomain.currentDomain.hasDefinition(name))
			{
				return null;
			}
			
			var cls:Class = ApplicationDomain.currentDomain.getDefinition(name) as Class;
			var instance:Object = new cls();
			
			return (instance is BitmapData) ? new Bitmap((instance as BitmapData), PixelSnapping.AUTO, true) : instance as DisplayObject;
		}
		
		/**
		 * 获取位图实例
		 * @param name 实例链接名
		 * @return
		 */
		public function getDefinitionBitmap(name:String):Bitmap
		{
			return this.getDefinitionDisplayObject(name) as Bitmap;
		}
		
		/**
		 * 获取影片剪辑实例
		 * @param name 实例链接名
		 * @return
		 */
		public function getDefinitionMovieClip(name:String):MovieClip
		{
			return this.getDefinitionDisplayObject(name) as MovieClip;
		}
		
		/**
		 * 获取Sprite实例
		 * @param name 实例链接名
		 * @return
		 */
		public function getDefinitionSprite(name:String):Sprite
		{
			return this.getDefinitionDisplayObject(name) as Sprite;
		}
		
		/**
		 * 获取音频实例
		 * @param name 实例链接名
		 * @return
		 */
		public function getDefinitionSound(name:String):Sound
		{
			var appDomain:ApplicationDomain = ApplicationDomain.currentDomain;
			
			if (!ApplicationDomain.currentDomain.hasDefinition(name))
			{
				return null;
			}
			
			var cls:Class = ApplicationDomain.currentDomain.getDefinition(name) as Class;
			var instance:Object = new cls();
			
			return instance as Sound;
		}
		
		/**
		 * 获取字体实例
		 * @param name 实例链接名
		 * @return
		 */
		public function getDefinitionFont(name:String):Font
		{
			var appDomain:ApplicationDomain = ApplicationDomain.currentDomain;
			
			if (!ApplicationDomain.currentDomain.hasDefinition(name))
			{
				return null;
			}
			
			var cls:Class = ApplicationDomain.currentDomain.getDefinition(name) as Class;
			var instance:Object = new cls();
			
			Font.registerFont(cls);
			
			return instance as Font;
		}
		
		/**
		 * 获取XML资源
		 * @param name 资源名称
		 * @return
		 */
		public function getXML(name:String):XML
		{
			if (!(this.getAsset(name) is XML))
			{
				try
				{
					return new XML(this.getAsset(name));
				}
				catch (err:Error)
				{
				}
			}
			
			return this.getAsset(name) as XML;
		}
		
		/**
		 * 获取图片数据资源
		 * @param name 资源名称
		 * @return
		 */
		public function getBitmapData(name:String):BitmapData
		{
			var asset:* = this.getAsset(name);
			
			if (asset is Bitmap)
			{
				return (asset as Bitmap).bitmapData;
			}
			
			return asset as BitmapData;
		}
		
		/**
		 * 获取图片资源
		 * @param name 资源名称
		 * @return
		 */
		public function getBitmap(name:String):Bitmap
		{
			var asset:* = this.getAsset(name);
			
			if (asset is BitmapData)
			{
				return new Bitmap(asset, PixelSnapping.AUTO, true);
			}
			
			return asset as Bitmap;
		}
		
		/**
		 * 获取影片剪辑
		 * @param name 资源名称
		 * @return
		 */
		public function getMovieClip(name:String):MovieClip
		{
			return this.getAsset(name) as MovieClip;
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		/**
		 * 初始化
		 */
		private function init():void
		{
			this.assets = new HashMap();
		}
	
	}
}
